package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        if (qo.getStatus()==null){
            return new TablePageInfo<Appointment>(appointmentMapper.selectForListAndNotDelete(qo));
        }
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }

    @Override
    public void save(Appointment appointment) {
        //前台传递过来的数据不能直接使用
        Appointment newObj = new Appointment();
        newObj.setCustomerName(appointment.getCustomerName());
        newObj.setCustomerPhone(appointment.getCustomerPhone());
        newObj.setAppointmentTime(appointment.getAppointmentTime());
        newObj.setLicensePlate(appointment.getLicensePlate());
        newObj.setCarSeries(appointment.getCarSeries());
        newObj.setServiceType(appointment.getServiceType());
        newObj.setInfo(appointment.getInfo());
        newObj.setCreateTime(new Date());
        appointmentMapper.insert(newObj);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //在预约中 状态下才能删除
            Appointment oldObj =this.get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
               throw new BusinessException("无法删除!!!");
            }else {
                appointmentMapper.deleteByPrimaryKey(dictId);
            }
        }
    }

    @Override
    public void update(Appointment appointment) {
        Appointment oldObj = this.get(appointment.getId());
        //只有预约中状态才能修改
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
                throw new BusinessException("只有状态为预约中的预约单才能进行编辑操作!");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void cancel(Long id) {
        Appointment oldObj =this.get(id);
        if (oldObj!=null){
            if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
                throw new BusinessException("无法取消");
            }
            appointmentMapper.updateStatus(id,Appointment.STATUS_CANCEL);
        }
    }

    @Override
    public void arrival(Long id) {
        Appointment oldObj =this.get(id);
        if (oldObj!=null){
            if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
                throw new BusinessException("无法到店");
            }
            //到店时间
            LocalDateTime actualArrivalTime = LocalDateTime.now();
            appointmentMapper.updateStatusById(oldObj.getId(),Appointment.STATUS_ARRIVAL,actualArrivalTime);
        }
    }

    @Override
    public void remove(Long[] ids) {
        if (ids != null && ids.length>0){
            for (Long id : ids) {
                Appointment oldObj = this.get(id);
                if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
                    throw new BusinessException("删除");
                }
                appointmentMapper.updateStatus(id,Appointment.STATUS_DELETE);
            }
        }
    }

    @Override
    public void changeStatus(Long appointmentId, Integer statusPaid) {
        appointmentMapper.changeStatus(appointmentId,statusPaid);
    }
}
